﻿using System;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Text;

/// <summary>
/// 邮件发送类
/// </summary>
public class SMTP
{
    private readonly string Sender = ConfigurationManager.AppSettings["Number"]; //发送方邮箱
    private readonly string SmtpServer = ConfigurationManager.AppSettings["Server"]; //服务器
    private readonly string To; //接收方邮箱账号
    private readonly string pwd = ConfigurationManager.AppSettings["Pwd"]; //发送方密码

    /// <summary>
    /// 邮件发送初始化
    /// </summary>
    /// <param name="To"></param>
    public SMTP(string To) //第1个参数：接收方邮箱账号
    {
        this.To = To;
    }

    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="subject">标题</param>
    /// <param name="bodyinfo">内容</param>
    /// <returns></returns>
    public bool sendemail(string subject, string bodyinfo)
    {
        var flag = false;
        var formto = Sender;
        var to = To;
        var content = subject;
        var body = bodyinfo;
        var name = Sender;
        var upass = pwd;
        var smtp = SmtpServer;
        var _smtpClient = new SmtpClient();
        _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; //指定电子邮件发送方式
        _smtpClient.Host = smtp; //指定SMTP服务器
        _smtpClient.Credentials = new NetworkCredential(name, upass); //用户名和密码
        var _mailMessage = new MailMessage();
        //发件人，发件人名 
        _mailMessage.From = new MailAddress(formto, "工艺品平台密码找回业务");
        //收件人 
        _mailMessage.To.Add(to);
        _mailMessage.SubjectEncoding = Encoding.GetEncoding("gb2312");
        _mailMessage.Subject = content; //主题

        _mailMessage.Body = body; //内容
        _mailMessage.BodyEncoding = Encoding.GetEncoding("gb2312"); //正文编码
        _mailMessage.IsBodyHtml = true; //设置为HTML格式
        _mailMessage.Priority = MailPriority.High; //优先级    
        _mailMessage.IsBodyHtml = true;
        try
        {
            _smtpClient.Send(_mailMessage);
            flag = true;
        }
        catch (Exception)
        {
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 邮箱激活
    /// </summary>
    /// <param name="url"></param>
    /// <param name="ID"></param>
    /// <returns></returns>
    public bool Activation(string url, string ID)
    {
        var flag = false;
        var formto = Sender;
        var to = To;
        var content = "工艺品平台:";
        var body = "尊敬的" + ID + "用户:请点击些链接激活:";
        body += "<a href=" + url + ">" + url + "</a>";
        var name = Sender;
        var upass = pwd;
        var smtp = SmtpServer; // "smtp.qq.com";
        var _smtpClient = new SmtpClient();
        _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; //指定电子邮件发送方式
        _smtpClient.Host = smtp; //指定SMTP服务器
        _smtpClient.Credentials = new NetworkCredential(name, upass); //用户名和密码
        var _mailMessage = new MailMessage();
        //发件人，发件人名 
        _mailMessage.From = new MailAddress(formto, "工艺品网站平台激活验证");
        //收件人 
        _mailMessage.To.Add(to);
        _mailMessage.SubjectEncoding = Encoding.GetEncoding("gb2312");
        _mailMessage.Subject = content; //主题

        _mailMessage.Body = body; //内容
        _mailMessage.BodyEncoding = Encoding.GetEncoding("gb2312"); //正文编码
        _mailMessage.IsBodyHtml = true; //设置为HTML格式
        _mailMessage.Priority = MailPriority.High; //优先级    
        _mailMessage.IsBodyHtml = true;
        try
        {
            _smtpClient.Send(_mailMessage);
            flag = true;
        }
        catch (Exception)
        {
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="subject"></param>
    /// <param name="body"></param>
    public void SendMail(string subject, string body) //第一个参数邮箱主题，第二个参数邮箱内容
    {
        var from = new MailAddress(Sender);

        var to = new MailAddress(To);

        var oMail = new MailMessage(from, to);
        oMail.Subject = subject; //主题
        oMail.Body = body; //内容

        oMail.IsBodyHtml = true;
        oMail.BodyEncoding = Encoding.GetEncoding("GB2312");
        oMail.Priority = MailPriority.High;
        var client = new SmtpClient();
        client.Host = SmtpServer; //指定邮件服务器
        client.Credentials = new NetworkCredential(Sender, pwd); //指定服务器邮件,及密码 


        //发送 
        try
        {
            client.Send(oMail); //发送邮件 
        }
        catch (Exception cp)
        {
            //发送不成功！
        }

        oMail.Dispose(); //释放资源 
    }
}